package com.mcjtf.prefix.service;

import com.google.common.collect.Maps;
import com.mcjtf.prefix.PrefixManager;
import com.mcjtf.prefix.model.User;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;

import java.util.List;
import java.util.Map;

public class PrefixService {
    private final PrefixManager plugin;
    private final Map<String, User> userMap = Maps.newConcurrentMap();

    public PrefixService() {
        plugin = PrefixManager.getInstance();
    }

    public void addPrefix(String name, List<String> prefix) {
        plugin.getConfigService().addPrefix(name, prefix);
    }

    public void addSuffix(String name, List<String> suffix) {
        plugin.getConfigService().addSuffix(name, suffix);
    }

    public void list(CommandSender sender, User user, String type) {
        int id = 0;
        if (type.equalsIgnoreCase("prefix") || type.equalsIgnoreCase("p")) {
            sender.sendMessage(plugin.msg("&e----------[ &6你拥有的前缀称号 &e]----------"));
            for (String data : user.getPrefixes()) {
                sender.sendMessage(plugin.msg("&a序号: &6" + id + "&a —— " + data));
                id++;
            }
        } else {
            sender.sendMessage(plugin.msg("&e----------[ &6你拥有的后缀称号 &e]----------"));
            for (String data : user.getSuffixes()) {
                sender.sendMessage(plugin.msg("&a序号: &6" + id + "&a —— " + data));
                id++;
            }
        }
        sender.sendMessage(ChatColor.YELLOW + "---------------------------------------");
    }

    public boolean del(User g, String type, Integer id) {
        String world = plugin.getServer().getWorlds().get(0).getName();
        return del(g, type, id, world);
    }

    public boolean del(User user, String type, Integer id, String world) {
        if (type.equalsIgnoreCase("prefix") || type.equalsIgnoreCase("p")) {
            if (user.getPrefixes().size() <= id) {
                return false;
            }
            user.removePrefix(id);
            if (user.getPrefixes().isEmpty()) {
                user.addPrefix(plugin.getVaultHook().getGroupPrefix(plugin.getVaultHook().getGroup(user.getName(), world), world));
            }
            if (user.getUsePrefix() == id) {
                user.setUsePrefix(0);
            }
            return true;
        } else if (type.equalsIgnoreCase("suffix") || type.equalsIgnoreCase("s")) {
            if (user.getSuffixes().size() <= id) {
                return false;
            }
            user.removeSuffix(id);
            if (user.getSuffixes().isEmpty()) {
                user.addSuffix(plugin.getVaultHook().getGroupSuffix(plugin.getVaultHook().getGroup(user.getName(), world), world));
            }
            if (user.getUseSuffix() == id) {
                user.setUseSuffix(0);
            }
            return true;
        }
        return false;
    }

    public User getUser(String name) {
        return userMap.get(name);
    }

    public Map<String, User> getUserMap() {
        return userMap;
    }

    public boolean loadPlayer(String name) {
        String world = Bukkit.getWorlds().get(0).getName();
        return loadPlayer(name, world);
    }

    public boolean loadPlayer(String name, String world) {
        if (userMap.containsKey(name)) {
            return true;
        }
        if (plugin.getConfigService().hasPlayer(name)) {
            return plugin.getConfigService().loadOfflinePlayer(name, world);
        }
        return false;
    }

    public void save() {
        plugin.getConfigService().saveAll();
    }

    public void removeData(String name) {
        plugin.getConfigService().dataCache.remove(name);
    }
}
